﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ImportPiket
{
    /// <summary>
    /// Логика взаимодействия для ImportPage3.xaml
    /// </summary>
    public partial class ImportPage3 : Page
    {
        /// <summary>
        /// Флаг - инициализация формы
        /// </summary>
        bool isInit;
        /// <summary>
        /// Количество колонок
        /// </summary>
        int MaxColumns = 10;
        /// <summary>
        /// Количество строк
        /// </summary>
        int MaxRows = 100;

        public ImportPage3()
        {
            isInit = false;
            InitializeComponent();
            isInit = true;
            Rebind();
        }

        /// <summary>
        /// Обновить ListBox
        /// </summary>
        void Rebind()
        {
            listBoxFields.ItemsSource = null;
            listBoxFields.ItemsSource = PrjProperty.fieldsList;
        }

        /// <summary>
        /// Кнопка Первый
        /// </summary>
        private void Button_Click_First(object sender, RoutedEventArgs e)
        {
            int noCur = PrjProperty.fieldsList.IndexOf(listBoxFields.SelectedItem as string);
            if (noCur > 0)
            {
                for (int i = noCur; i > 0; i--)
                    PrjProperty.Swap(PrjProperty.fieldsList, i, i - 1);
            }

            Rebind();
            listBoxFields.SelectedIndex = 0;

        }

        /// <summary>
        /// Кнопка Шаг назад
        /// </summary>
        private void Button_Click_Prew(object sender, RoutedEventArgs e)
        {
            int noCur = PrjProperty.fieldsList.IndexOf(listBoxFields.SelectedItem as string);
            if (noCur > 0)
            {
                PrjProperty.Swap(PrjProperty.fieldsList, noCur, noCur - 1);
                Rebind();
                listBoxFields.SelectedIndex = noCur - 1;
            }
        }

        /// <summary>
        /// Кнопка Шаг вперед
        /// </summary>
        private void Button_Click_Next(object sender, RoutedEventArgs e)
        {
            int noCur = PrjProperty.fieldsList.IndexOf(listBoxFields.SelectedItem as string);
            if (noCur < PrjProperty.fieldsList.Count - 1)
            {
                PrjProperty.Swap(PrjProperty.fieldsList, noCur, noCur + 1);
                Rebind();
                listBoxFields.SelectedIndex = noCur + 1;
            }
        }

        /// <summary>
        /// Кнопка В конец
        /// </summary>
        private void Button_Click_Last(object sender, RoutedEventArgs e)
        {
            int noCur = PrjProperty.fieldsList.IndexOf(listBoxFields.SelectedItem as string);
            if (noCur < PrjProperty.fieldsList.Count - 1)
            {
                for (int i = noCur; i < PrjProperty.fieldsList.Count - 1; i++)
                    PrjProperty.Swap(PrjProperty.fieldsList, i, i + 1);
            }
            
            Rebind();
            listBoxFields.SelectedIndex = PrjProperty.fieldsList.Count - 1;
        }

        /// <summary>
        /// Взять из файла
        /// </summary>
        private void Button_Click_GetFile(object sender, RoutedEventArgs e)
        {
            if (PrjProperty.typeFile == 0)
            {
                string stext = PrjProperty.txtData + "\r\n";
                using (System.IO.StringReader reader = new System.IO.StringReader(stext))
                {
                    string line;
                    //читает текст по строчно
                    List<string> listValue = new List<string>();
                    //находит строку со словом "from" и от него набирает слова в список
                    while ((line = reader.ReadLine()) != null)
                    {
                        line = line.ToLower();
                        if (line.IndexOf("from") > -1)
                        {
                            line += " ";
                            string str = "";
                            foreach (char ch in line)
                            {
                                if (ch != ' ')
                                {
                                    str += ch.ToString();
                                }
                                else
                                {
                                    if (str != "")
                                    {
                                        listValue.Add(str);
                                        str = "";
                                    }
                                }
                            }
                        }
                    }
                    if (listValue.Count > 8)
                    {
                        var fieldsList = new List<string>();
                        // FROM           TO   LENGTH  BEARING      INC     LEFT       UP     DOWN    RIGHT   FLAGS  COMMENTS
                        foreach (string sfield in listValue)
                        {
                            switch (sfield)
                            {
                                case "from": 
                                    fieldsList.Add("От");
                                    break;
                                case "to": 
                                    fieldsList.Add("До");
                                    break;
                                case "length":
                                    fieldsList.Add("Длина");
                                    break;
                                case "bearing":
                                    fieldsList.Add("Азимут");
                                    break;
                                case "inc":
                                    fieldsList.Add("Угол");
                                    break;
                                case "left":
                                    fieldsList.Add("Лево");
                                    break;
                                case "up":
                                    fieldsList.Add("Верх");
                                    break;
                                case "down":
                                    fieldsList.Add("Низ");
                                    break;
                                case "right":
                                    fieldsList.Add("Право");
                                    break;
                            }
                        }
                        if (fieldsList.Count == 9)
                        {
                            PrjProperty.fieldsList = new List<string>(fieldsList);
                        }
                        Rebind();
                    }
                }
            }
        }


    }
}
